// 工具函数
export const utils = {
  // 格式化价格
  formatPrice: (price) => {
    return new Intl.NumberFormat('zh-CN', {
      style: 'currency',
      currency: 'CNY'
    }).format(price)
  },
  
  // 格式化日期
  formatDate: (date) => {
    return new Date(date).toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  },
  
  // 格式化相对时间
  formatRelativeTime: (date) => {
    const now = new Date()
    const target = new Date(date)
    const diff = now - target
    
    const minutes = Math.floor(diff / (1000 * 60))
    const hours = Math.floor(diff / (1000 * 60 * 60))
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
    
    if (minutes < 1) return '刚刚'
    if (minutes < 60) return `${minutes}分钟前`
    if (hours < 24) return `${hours}小时前`
    if (days < 7) return `${days}天前`
    
    return utils.formatDate(date)
  },
  
  // 段位颜色映射
  getRankColor: (rank) => {
    const colorMap = {
      '无段位': '#666666',
      '青铜': '#CD7F32',
      '白银': '#C0C0C0',
      '黄金': '#FFD700',
      '铂金': '#E5E4E2',
      '钻石': '#B9F2FF',
      '黑鹰': '#2F4F4F',
      '三角洲巅峰': '#FF4500'
    }
    return colorMap[rank] || '#666666'
  },
  
  // 状态颜色映射
  getStatusColor: (status) => {
    const colorMap = {
      '待审核': '#faad14',
      '已上架': '#52c41a',
      '已下架': '#d9d9d9',
      '已出租': '#722ed1',
      '已消耗': '#8c8c8c',
      '审核失败': '#f5222d',
      '进行中': '#1890ff',
      '待确认': '#fa8c16',
      '已完成': '#52c41a',
      '申诉中': '#eb2f96',
      '审核通过': '#52c41a',
      '审核驳回': '#f5222d'
    }
    return colorMap[status] || '#666666'
  },
  
  // 游戏类型映射
  getGameIcon: (game) => {
    const iconMap = {
      '王者荣耀': '👑',
      '和平精英': '🔫',
      '英雄联盟': '⚔️',
      '原神': '🌟',
      '穿越火线': '🔥',
      'DOTA2': '🗡️'
    }
    return iconMap[game] || '🎮'
  },
  
  // 验证邮箱格式
  isValidEmail: (email) => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    return emailRegex.test(email)
  },
  
  // 验证手机号格式
  isValidPhone: (phone) => {
    const phoneRegex = /^1[3-9]\d{9}$/
    return phoneRegex.test(phone)
  },
  
  // 生成随机ID
  generateId: () => {
    return Date.now().toString(36) + Math.random().toString(36).substr(2)
  },
  
  // 截断文本
  truncateText: (text, maxLength = 50) => {
    if (!text) return ''
    return text.length > maxLength ? text.slice(0, maxLength) + '...' : text
  },
  
  // 防抖函数
  debounce: (func, wait) => {
    let timeout
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout)
        func(...args)
      }
      clearTimeout(timeout)
      timeout = setTimeout(later, wait)
    }
  },
  
  // 节流函数
  throttle: (func, limit) => {
    let inThrottle
    return function(...args) {
      if (!inThrottle) {
        func.apply(this, args)
        inThrottle = true
        setTimeout(() => inThrottle = false, limit)
      }
    }
  }
} 